package com.sxpi.chapter02.mode.factory;

import com.sxpi.chapter02.mode.IUserVipService;
import com.sxpi.chapter02.mode.NormalVipUserService;
import com.sxpi.chapter02.mode.SvipUserService;
import com.sxpi.chapter02.mode.YearvipUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author: 董伟豪
 * @Date: 2023/2/4 16:53
 */
@Component
public class SpringUserBeanFactory implements ApplicationContextAware {


    private static final Logger log = LoggerFactory.getLogger(SpringUserBeanFactory.class);


    private static ApplicationContext applicationContext;

    /**
     * 简单工厂设计模式
     *
     * @param vip
     * @return
     */
    public static IUserVipService getInstance(Integer vip) {
        if (vip.equals(1)) {
            return applicationContext.getBean(NormalVipUserService.class);
        } else if (vip.equals(2)) {
            return applicationContext.getBean(YearvipUserService.class);
        } else if (vip.equals(3)) {
            return applicationContext.getBean(SvipUserService.class);
        }
        return null;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUserBeanFactory.applicationContext = applicationContext;
    }
}
